﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace Aandelendelen.Payment
{
	public class IdealFetch : Ideal
	{
		string transactionId;
		int amount;
		string currency;
		string url;
		string message;
		string custom = "";

		public IdealFetch(string partnerId, string description, string reportUrl, string returnUrl, string bankId, decimal amount)
			: this(partnerId, false, description, reportUrl, returnUrl, bankId, amount)
		{
		}

		public IdealFetch(string partnerId, bool testMode, string description, string reportUrl, string returnUrl, string bankId, decimal amount)
			: base(partnerId, testMode)
		{
			Dictionary<string, string> args = new Dictionary<string, string>();

			args["rtlo"] = this.partnerId;
			args["bank"] = bankId;


			//args["partnerid"] = this.partnerId;
			args["description"] = description;
			args["reporturl"] = reportUrl;
			args["returnurl"] = returnUrl;
			args["amount"] = amount.ToString("#0");

			logger.Debug("Amount: {0}", args["amount"]);
			//args["bank_id"] = bankId;

			this.ProcessRequest(this.fetchUrl, args);
		}

		protected override void ProcessTargetPayResponse(string response)
		{
			if (response == null)
				throw new Exception("response is null");

			try
			{
				string returnCode = response.Substring(0, 6);
				if (returnCode != "000000")
					throw new Exception("FetchError " + returnCode);

				this.transactionId = response.Substring(7, 16);
				this.url = response.Substring(24);
			}
			catch (Exception e)
			{
				throw new Exception("Error parsing response: '" + response + "'", e);
			}

		}

		protected override void ProcessResponse(XmlDocument doc)
		{
			XmlNode orderNode = doc.DocumentElement.FirstChild;

			if (orderNode.Name.ToLower() != "order")
				this.ThrowXmlException(doc);

			foreach (XmlNode childNode in orderNode)
			{
				switch (childNode.Name.ToLower())
				{
					case "transaction_id":
						this.transactionId = childNode.InnerText;
						break;
					case "amount":
						//write the outerxml until we found the bug causing the ToInt32 to fail sometimes.
						logger.Debug(childNode.OuterXml);
						this.amount = XmlConvert.ToInt32(childNode.InnerText);
						break;
					case "currency":
						this.currency = childNode.InnerText;
						break;
					case "url":
						this.url = childNode.InnerText;
						break;
					case "message":
						this.message = childNode.InnerText;
						break;
					default: this.custom += childNode.OuterXml;
						break;
				}
			}
		}

		public string TransactionId
		{
			get { return this.transactionId; }
		}

		public string Currency
		{
			get { return this.currency; }
		}

		public int Amount
		{
			get { return this.amount; }
		}

		public string Url
		{
			get { return this.url; }
		}

		public string Message
		{
			get { return this.message; }
		}

		public string Custom
		{
			get { return this.custom; }
		}

	}
}